﻿using System;
using System.Linq;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using Core = Vision.Core;

namespace Vision.Core.ActivityComponents.Activities
{
    /// <summary>
    /// 
    /// </summary>
    public class ExecuteVisionActivity : CodeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> CatalogConnection { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        public InArgument<string> ActivityKey { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        public InArgument<Dictionary<string, object>> InputParameters { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public OutArgument<Dictionary<string, object>> OutputParameters { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(CodeActivityContext context)
        {
            Entities.Activities.VisionActivityParameterCollection activityOutParameters = new Entities.Activities.VisionActivityParameterCollection();
            Entities.Activities.VisionActivityParameterCollection activityInputParameters = new Entities.Activities.VisionActivityParameterCollection();
            if(this.InputParameters.Get(context) != null){
                foreach(string key in this.InputParameters.Get(context).Keys){
                    activityInputParameters.Add(key, this.InputParameters.Get(context)[key]);
                }
            }
         
            Core.Entities.Activities.VisionActivity visionActivity;
            Guid activityKey = Guid.Parse(this.ActivityKey.Get(context));
            using (Core.Components.ActivityCatalog activityCatalog = new Core.Components.ActivityCatalog(this.CatalogConnection.Get(context))){
                visionActivity = activityCatalog.GetVisionActivityFromCatalog(activityKey);
                using (Core.Components.ActivityExecuter activityExecuter = new Core.Components.ActivityExecuter()){
                    activityExecuter.ExecuteVisionActivity(visionActivity, activityInputParameters, out activityOutParameters);                    
                }
            }

            if (activityOutParameters != null){
                this.OutputParameters.Set(context, activityOutParameters.ToDictionary());
            }
        }
    }
}
